package com.weilian.ordering.common;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Handler;
import android.os.Message;
import android.view.MotionEvent;
import android.view.GestureDetector.OnGestureListener;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.ViewFlipper;

import com.weilian.ordering.beans.PageBean;
import com.weilian.ordering.beans.TypeBean;
import com.weilian.ordering.util.CacheManager;
import com.weilian.ordering.util.Util;

/**
 * PageControl about the page change and same details.
 * 
 * @author Sojex
 * 
 */
public class PageControl implements OnGestureListener {
	private ViewFlipper flipper;
	private Context context;
	private Handler handler;
	private int currentPage = 0, pageCount = 0;
	private TypeBean typeBean;

	public PageControl(ViewFlipper flipper, TypeBean typeBean, Context context,
			Handler handler) {
		// TODO Auto-generated constructor stub
		this.flipper = flipper;
		this.context = context;
		this.handler = handler;
		this.typeBean = typeBean;
		pageCount = typeBean.list.size();
		Message msg = new Message();
		msg.what = 3;
		msg.arg1 = currentPage + 1;
		msg.arg2 = pageCount;
		msg.obj = typeBean.typeName;
		handler.sendMessage(msg);
		flipper.removeAllViews();
		addView(0);// add first page
	}

	private Handler mHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			System.out.println("Change types typeId:" + msg.arg2);
			typeBean = (TypeBean) msg.obj;
			currentPage = 0;
			pageCount = typeBean.list.size();
			Message msg1 = new Message();
			msg1.what = 3;
			msg1.arg1 = currentPage + 1;
			msg1.arg2 = pageCount;
			msg1.obj = typeBean.typeName;
			handler.sendMessage(msg1);
			switch (msg.what) {
			case 0:
				flipper.removeAllViews();
				addView(0);// first page
				// style and
				// page position
				break;
			case 1:
				// pageMove(Style.PREVIOUS, msg.arg1,0);
				break;
			}
		};
	};

	/**
	 * get PageControl Handler
	 * 
	 * @return Handler
	 */
	public Handler getHandler() {
		return mHandler;
	}

	/**
	 * add page view
	 * 
	 * @param templetId
	 * @param typeId
	 * @param pageId
	 */
	private void addView(int pagePositon) {
		ImageView imageView = new ImageView(context);
		PageBean bean = typeBean.list.get(pagePositon);
		imageView.setBackgroundDrawable(new BitmapDrawable(
				loadBitmapFromSDCard(bean.imagePath)));
		 flipper.addView(imageView);
	}

	/**
	 * page move method
	 * 
	 * @param direction
	 * @param templetId
	 * @param typeId
	 * @param pageId
	 */
	public void pageMove(int direction, int pageId) {
		System.out.println("pageId:" + pageId);
		addView(pageId);
		switch (direction) {
		case Style.NEXT: // next page
			flipper = Style.turnPage(context, 0 + 1, flipper);// set next page
			// style
			flipper.showNext();
			break;
		case Style.PREVIOUS: // previous page
			flipper = Style.turnPage(context, -(0 + 1), flipper);// set previous
			// page
			// style
			flipper.showPrevious();
			break;
		}
		flipper.removeViewAt(0); // remove first page
	}

	@Override
	public boolean onDown(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {
		// TODO Auto-generated method stub
		if (e1.getX() - e2.getX() > 120) {// next page
			if (currentPage + 1 < pageCount) {
				++currentPage;
				pageMove(Style.NEXT, currentPage);
				Message msg = new Message();
				msg.what = 3;
				msg.arg1 = currentPage + 1;
				msg.arg2 = pageCount;
				msg.obj = typeBean.typeName;
				handler.sendMessage(msg);
				if (currentPage == (pageCount - 1)) {
					handler.sendEmptyMessage(0);// last page
				} else {
					handler.sendEmptyMessage(1);// middle page
				}
			}else{
				Toast.makeText(context, "这是最后一页", 0).show();
			}
			return true;
		} else if (e1.getX() - e2.getX() < -120) {// previous page
			if (currentPage - 1 > -1) {
				--currentPage;
				pageMove(Style.PREVIOUS, currentPage);
				Message msg = new Message();
				msg.what = 3;
				msg.arg1 = currentPage + 1;
				msg.arg2 = pageCount;
				msg.obj = typeBean.typeName;
				handler.sendMessage(msg);
				if (currentPage == 0) {
					handler.sendEmptyMessage(2);
				} else {
					handler.sendEmptyMessage(1);
				}
			}else{
				Toast.makeText(context, "这是第一页", 0).show();
			}
			return true;
		}
		return false;
	}

	@Override
	public void onLongPress(MotionEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
			float distanceY) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void onShowPress(MotionEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean onSingleTapUp(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}

	public Bitmap loadBitmapFromSDCard(String path) {
		if (CacheManager.check(path) && CacheManager.get(path) != null) {
			return CacheManager.get(path);
		} else {
			return Util.loadBitmapFromSDCard(path).get();
		}
	}
}
